package com.example.exam.controller;

import com.example.exam.entity.ExamineeInfo;
import com.example.exam.entity.ManagerInfo;
import com.example.exam.req.RegisterReq;
import com.example.exam.req.admin.AdminAddexam;
import com.example.exam.req.admin.AdminLoginReq;
import com.example.exam.req.admin.UpdateExamineeInfo;
import com.example.exam.resp.CommonResp;
import com.example.exam.resp.admin.AdminInfo;
import com.example.exam.resp.admin.FindExaminee;
import com.example.exam.service.impl.ExamineeInfoServiceImpl;
import com.example.exam.service.impl.ManagerInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

@ResponseBody
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private ManagerInfoServiceImpl managerInfoService;

    @Autowired
    private ExamineeInfoServiceImpl examineeInfoService;

    /**
     * 登录
     * @param adminLoginReq
     * @return
     */
    @PostMapping("/login")
    public CommonResp login(@RequestBody AdminLoginReq adminLoginReq) {
        CommonResp resp = new CommonResp();
        ManagerInfo managerInfo;
        AdminInfo adminInfo = new AdminInfo();
        if (adminLoginReq.getManagerUsername() != null && adminLoginReq.getManagerPwd() != null) {
            managerInfo = managerInfoService.login(adminLoginReq);
            if (managerInfo != null) {
                adminInfo.setManagerId(managerInfo.getManagerId());
                adminInfo.setManagerName(managerInfo.getManagerName());
                adminInfo.setManagerPrivilege(managerInfo.getManagerPrivilege());
                resp.setMessage("登录成功");
                resp.setContent(adminInfo);
                return resp;
            } else {
                resp.setSuccess(false);
                resp.setMessage("登录失败,身份不对");
                return resp;
            }
        }
        resp.setSuccess(false);
        resp.setMessage("登录失败");
        return resp;
    }

    /**
     * 查找所有考生
     * @return
     */
    @GetMapping("/findAllExaminee")
    public CommonResp findAllExaminee() {
        CommonResp resp = new CommonResp();
        List<ExamineeInfo> list = managerInfoService.findAllExaminee();
        if (list.size() > 0) {
            resp.setContent(list);
            return resp;
        }
        resp.setMessage("暂无用户信息");
        return resp;
    }

    /**
     * 根据身份证号删除用户
     * @param IDCard
     * @return
     */
    @GetMapping("/deleteByIDCard/{IDCard}")
    public CommonResp deleteByIDCard(@PathVariable("IDCard") String IDCard) {
        CommonResp resp = new CommonResp();
        boolean flag = managerInfoService.deleteByIDCard(IDCard);
        if (flag) {
            resp.setMessage("删除成功");
            return resp;
        }
        resp.setSuccess(false);
        resp.setMessage("删除失败，未找到该用户");
        return resp;
    }

    /**
     * 增加考生用户
     * @param registerReq
     * @return
     */
    @PostMapping("/addExaminee")
    public CommonResp addExaminee(@RequestBody RegisterReq registerReq) {
        CommonResp resp = new CommonResp();
        int num = examineeInfoService.getNumByIDCard(registerReq.getExamineeIDCard());
        int count = examineeInfoService.getCountIDCard(registerReq.getExamineeIDCard());
        if (num > 0) {
            resp.setSuccess(false);
            resp.setMessage("该身份证号已经被注册过，请直接登录！");
        } else if (count != 0) {
            examineeInfoService.update(registerReq);
            resp.setMessage("创建成功");
        } else {
            examineeInfoService.register(registerReq);
            resp.setMessage("创建成功");
        }
        return resp;
    }

    /**
     * 更新用户信息
     * @param updateExamineeInfo
     * @return
     */
    @PostMapping("/updateExamineeInfo")
    public CommonResp updateExamineeInfo(@RequestBody UpdateExamineeInfo updateExamineeInfo) {
        CommonResp resp = new CommonResp();
        boolean flag = managerInfoService.updateExamineeInfo(updateExamineeInfo);
        if (flag) {
            resp.setMessage("更新成功");
            return resp;
        }
        resp.setMessage("更新失败");
        return resp;
    }

    /**
     * 发布考试
     * @param adminAddexam
     * @return
     */
    @PostMapping("/addExam")
    public CommonResp addExam(@RequestBody AdminAddexam adminAddexam) {
        CommonResp resp = new CommonResp();
        boolean flag = managerInfoService.adminAddexam(adminAddexam);
        if (flag) {
            resp.setMessage("考试已发布");
            return resp;
        } else {
            resp.setSuccess(false);
            resp.setMessage("考试发布失败");
            return resp;
        }
    }
}
